package Login;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 故我
 * Date: 2023-10-25
 * Time: 13:22
 */
@WebServlet("/index")
public class IndexServlet extends HttpServlet {
    //重定向传过来的是Get请求
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //先判断用户是否是登录状态，sessionId中是否存在
        HttpSession session = req.getSession(false);
        if (session == null) {
            //没有登录，去登录
            System.out.println("用户未登录!");
            resp.sendRedirect("login.html");
            return;
        }
        //根据HttpSession对象，获取到存储的username  默认是object对象
        String username = (String) session.getAttribute("username");
        resp.setContentType("text/html;charset=utf8");
        resp.getWriter().write("你好 " + username + " 叽叽叽");
    }
}
